﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace BasharLulu.QuranBookmark.WP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.SuraViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.UpdateValues();
            base.OnNavigatedTo(e);
        }

        private void UpdateValues()
        {
            this.HistoryListBox.ItemsSource = null;
            this.HistoryListBox.ItemsSource = AppSettings.LoadData();

            if (this.HistoryListBox.Items.Count > 0)
            {
                this.CurrentAyaNumber.Text = string.Format("Verse {0}", ((HistoryItem)this.HistoryListBox.Items[0]).AyaKey);
                this.CurrentSuraArabicName.Text = string.Format("{0}", ((HistoryItem)this.HistoryListBox.Items[0]).ArabicName);
            }
            else
            {
                this.CurrentAyaNumber.Text = " ";
                this.CurrentSuraArabicName.Text = " ";
            }
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.SuraViewModel.IsDataLoaded)
            {
                App.SuraViewModel.LoadData();
            }

            //this.HistoryListBox.ItemsSource = AppSettings.LoadData();
        }


        private void btnSetBookmark_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Bookmark.xaml", UriKind.Relative));
        }

        private void btnClearHistory_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult m = MessageBox.Show("Are you sure you want to clear the bookmark history?", "Confirmation", MessageBoxButton.OKCancel);
            
            if (m == MessageBoxResult.Cancel)
                return;

            AppSettings.ClearData();

            this.UpdateValues();
        }
    }
}